package com.bm.util;

import cn.hutool.crypto.digest.DigestUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 86134
 * @version 1.0
 * @description TODO
 * @date 2024/3/28 19:45
 */

public class CommunityUtil {
    public static char[] dictionaries = {
            'a',
            'b',
            'c',
            'd',
            'e',
            'f',
            'g',
            'h',
            'i',
            'j',
            'k',
            'l',
            'm',
            'n',
            'o',
            'p',
            'q',
            'r',
            's',
            't',
            'u',
            'v',
            'w',
            'x',
            'y',
            'z',
            '~',
            '!',
            '@',
            '#',
            '$',
            '%',
            '&',
            '*'
    };

    public static String encrypt(List<Long> buildingIds, int authenticatedType, int userId){
        Long stamp = DateUtils.dateConversion();
        StringBuffer ret = new StringBuffer();
        ret.append(stamp);
        ret.append(authenticatedType);
        ret.append(pad(userId,19));
        for (Long buildingId : buildingIds) {
            ret.append(pad(Integer.parseInt(buildingId+""),19));
        }
        ret.append(fake(8));
        return DigestUtil.md5Hex(ret.toString());
    }

    //生成id
    public static String pad(int num, int len) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len - (num + "").length(); i++) {
            sb.append(dictionaries[Integer.valueOf((Math.round(Math.random() * (dictionaries.length - 1)))+"")]);
        }
        sb.append(num);
        return sb.toString();
    }

    //生成位数密码
    public static String fake(int len){
        StringBuffer ret = new StringBuffer();
        for (int i = 0; i < len; i++) {
            ret.append(dictionaries[Integer.valueOf((Math.round(Math.random() * (dictionaries.length - 1))) + "")]);
        }
        return ret.toString();
    }
}
